package nl.cmkprojects.sage.web;

import java.text.BreakIterator;

import org.apache.commons.lang.StringEscapeUtils;

public class InfoFormatter {

	public static void main(String[] args) {
		String text = "John Smith stopped by earlier "
				+ "to say 'Happy birthday!' Aren't "
				+ "you and &#151;   he&#39; the , same age? He and "
				+ "his wife have\n 2.5 children.\n   \n Hello.";
		System.out.println(InfoFormatter.formatText(text, "\n"));
	}
	
	public static String formatText(String text, String sentenceSeperator)
	{
		String untext = text.replaceAll("&#151;", "--");
		untext = StringEscapeUtils.unescapeHtml(untext);
		StringBuffer sb = new StringBuffer();
		BreakIterator bi = BreakIterator.getSentenceInstance();
		bi.setText(untext);
		int index = 0;
		while (bi.next() != BreakIterator.DONE) {
			if (index > 0)
			{
				sb.append(sentenceSeperator);
			}
			String sentence = untext.substring(index, bi.current());
			sb.append(formatSentence(sentence));
			index = bi.current();
		}
		return sb.toString();
	}
	private static String formatSentence(String s)
	{
		String result = s.replaceAll("\n", " ");
		return result;
	}
}
